// app.js
import {
    request_post
} from './utils/request';
App({
    onLaunch() {
        wx.onMemoryWarning(function (res) {
            if (res.level === 5) {
                wx.showToast({
                    title: '内存不足，请关闭部分应用程序',
                })
            }
        })
    },
    onShow() {
        this.globalData.startTime = new Date().getTime()
        this.tryLogin();
        this.versionsUpdate();
    },
    onHide() {
        if (wx.getStorageSync('studentId')) {
            request_post('/student/increase-used-time', {
                usedTimeSecond: parseInt((new Date().getTime() - this.globalData.startTime) / 1000)
            })
        }
    },
    tryLogin(cb) {
        wx.login({
            success: res => {
                request_post('/mini-app/auth/login/code', {
                    code: res.code,
                }).then(res => {
                    if (res.id) {
                        wx.setStorageSync('studentId', res.id);
                        this.globalData.student = res
                    } else {
                        this.globalData.student = null
                        wx.removeStorageSync('studentId')
                    }

                    if (res.openId) wx.setStorageSync('openId', res.openId);
                    else wx.removeStorageSync('openId')
                    if (cb) return cb(res)
                    this.getCurrPage().onLoad()
                    this.getCurrPage().onShow()
                })
            }
        })
    },
    doLogin(cbOptions) { //重新登录
        //清空Storage
        wx.clearStorageSync();
        this.tryLogin(res => {
            if (res.id) {
                this.getCurrPage().onLoad(cbOptions)
                this.getCurrPage().onShow(cbOptions)
                return
            }
            wx.navigateTo({
                url: '/pages/login/index?redirect=/' + encodeURIComponent(this.getCurrRoute())
            })
        })
    },
    //版本更新
    versionsUpdate() {
        const updateManager = wx.getUpdateManager();
        updateManager.onUpdateReady(function () {
            wx.showModal({
                title: '更新提示',
                showCancel: false,
                content: '新版本已经准备好，是否重启应用？',
                success(res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                },
            })
        })
    },
    getCurrPage: function () {
        var pages = getCurrentPages(); //获取加载的页面
        return pages[pages.length - 1]; //获取当前页面的对象
    },
    getCurrRoute: function () {
        let currentPage = this.getCurrPage();
        var url = currentPage.route; //当前页面url
        var options = currentPage.options; //获取url中所带的参数
        //拼接url的参数
        currentPage = url + '?';
        for (var key in options) {
            var value = options[key]
            currentPage += key + '=' + value + '&';
        }
        currentPage = currentPage.substring(0, currentPage.length - 1);
        return currentPage;
    },
    globalData: {
        student: null,
        startTime: new Date().getTime()
    }
})